package com.yunkaweilai.android.activity;

import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.provider.Settings;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.github.lazylibrary.util.NetWorkUtils;
import com.github.lazylibrary.util.PreferencesUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.login.LoginActivity;
import com.yunkaweilai.android.base.BaseActivity;
import com.yunkaweilai.android.net.Glide.GlideUtils;
import com.yunkaweilai.android.utils.StatusBarUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/26.
 */

public class SplashActivity extends BaseActivity {

    @BindView(R.id.sp_bg)
    ImageView spBg;
    @BindView(R.id.sp_jump_btn)
    Button spJumpBtn;
    public static final String APP_VERSION_CODE = "APP_VERSION_CODE";
    private boolean flag = true;
    private boolean trimFlag = true;
    private CountDownTimer countDownTimer = new CountDownTimer(3200, 1000) {
        @Override
        public void onTick(long millisUntilFinished) {
            spJumpBtn.setText(millisUntilFinished / 1000 + "秒跳过");
        }

        @Override
        public void onFinish() {
            spJumpBtn.setText(0 + "秒跳过");
            if (flag) {
                gotoLoginOrMainActivity();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
            finish();
            return;
        }
        setContentView(R.layout.activity_splash);
        ButterKnife.bind(this);
        StatusBarUtil.setStatusBarTranslucent(this, false);
        spBg.setScaleType(ImageView.ScaleType.CENTER_CROP);
        GlideUtils.loadImage(mAc, "http://www.91hyk.com/AppImg/welcome.jpg", spBg, false);
        int networkType = NetWorkUtils.getNetworkType(mAc);
        if (networkType == -1) {
            startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
        } else {
            countDownTimer.start();
            spJumpBtn.setVisibility(View.VISIBLE);
        }

    }

    //跳转
    private void gotoLoginOrMainActivity() {
        int versionCode = 0;
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            versionCode = info.versionCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 首次进入跳转到引导页
        int anInt = PreferencesUtils.getInt(mAc, APP_VERSION_CODE, 0);
        if (versionCode > anInt) {
            PreferencesUtils.putInt(mAc, APP_VERSION_CODE, versionCode);
            intent2Activity(GuideActivity.class);
            finish();
        } else {
            intent2Activity(LoginActivity.class);
            finish();
        }
        flag = false;
    }

    @OnClick(R.id.sp_jump_btn)
    public void onViewClicked() {
        if (flag) {
            gotoLoginOrMainActivity();
        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if (!trimFlag) {
            countDownTimer.start();
            spJumpBtn.setVisibility(View.VISIBLE);
        }
        trimFlag = false;
    }

    @Override
    public void onPause() {
        super.onPause();
//        Glide.clear(spBg);
    }
}
